package com.easy.component.network.request;


import com.easy.component.network.EasyNetWork;
import com.easy.component.network.api.ApiService;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

/**
 * 描述: {所有请求的基类}
 * 作者:
 * 创建日期: 2017/9/11 21:34
 * 修改日期: 2017/9/11
 * 版本: {版本}
 */
public abstract class BaseRequest<T extends BaseRequest> {
    /**
     * 请求方式
     */
    protected int method = Method.POST;
    /**
     *
     */
    protected OkHttpClient okHttpClient;
    /**
     *
     */
    protected Retrofit retrofit;
    /**
     * BaseUrl
     */
    protected String baseUrl;
    /**
     * 请求url
     */
    protected String url;
    /**
     * 读超时
     */
    protected long readTimeOut;
    /**
     * 写超时
     */
    protected long writeTimeOut;
    /**
     * 链接超时
     */
    protected long connectTimeout;
    /**
     * 通用的的api接口
     */
    protected ApiService apiManager;

    public BaseRequest(String url) {
        this.baseUrl = EasyNetWork.getInstance().getBaseUrl();
        this.url = url;
    }

    /**
     * 设置读超时
     *
     * @param readTimeOut
     */
    public T setReadTimeOut(long readTimeOut) {
        this.readTimeOut = readTimeOut;
        return (T) this;
    }

    /**
     * 设置写入时间
     *
     * @param writeTimeOut
     */
    public T setWriteTimeOut(long writeTimeOut) {
        this.writeTimeOut = writeTimeOut;
        return (T) this;
    }

    /**
     * 设置链接超时
     *
     * @param connectTimeout
     */
    public void setConnectTimeout(long connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public T build() {
        OkHttpClient.Builder okHttpClientBuilder = buildOkttp();
        Retrofit.Builder retrofitBuilder = buildRetrofit();
        okHttpClient = okHttpClientBuilder.build();
        retrofitBuilder.client(okHttpClient);
        retrofit = retrofitBuilder.build();
        apiManager = retrofit.create(ApiService.class);
        return (T) this;
    }

    /**
     * 获取okhttp
     *
     * @return
     */
    public OkHttpClient.Builder buildOkttp() {
        if (readTimeOut <= 0 && writeTimeOut <= 0 && connectTimeout <= 0) {
            OkHttpClient.Builder builder = EasyNetWork.getOkHttpClientBuilder();
            return builder;
        } else {
            OkHttpClient.Builder newClientBuilder = EasyNetWork.resetOkHttpClient().newBuilder();
            if (readTimeOut > 0) {
                newClientBuilder.readTimeout(readTimeOut, TimeUnit.MILLISECONDS);
            }
            if (writeTimeOut > 0) {
                newClientBuilder.writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS);
            }
            if (connectTimeout > 0) {
                newClientBuilder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS);
            }
            return newClientBuilder;
        }
    }

    /**
     * @return
     */
    public Retrofit.Builder buildRetrofit() {
        return EasyNetWork.getRetrofitBuilder().baseUrl(baseUrl);
    }

    public interface Method {
        int GET = 0;
        int POST = 1;
    }

}
